﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;

using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BLToolkit.Data;
using BLToolkit.Data.Sql;

namespace PlatformManagement
{
    public partial class BackupForm : DevComponents.DotNetBar.Metro.MetroForm
    {
        public BackupForm()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSaveLocation_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "(*.bak)|*.bak";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                txtLocation.Text = saveFileDialog1.FileName;
                btnBackup.Enabled = true;
            }
        }

        private void btnBackup_Click(object sender, EventArgs e)
        {
            try
            {
                var db = new DbManager("DBConnection");
                db.SetCommand("BACKUP DATABASE "+cmbDb.Text+" TO DISK ='" + saveFileDialog1.FileName + "'WITH NOFORMAT,NOINIT,SKIP,STATS = 10");
                db.ExecuteNonQuery();
                MessageBox.Show("Compeleted successfully, your file saved in " + saveFileDialog1.FileName);
            }

            catch (SqlException ex)
            {
                MessageBox.Show("Lỗi: " + ex.Message);

            }
            catch (Exception ex1)
            {
                MessageBox.Show("Lỗi: " + ex1.Message);

            }
        }

        private void BackupForm_Load(object sender, EventArgs e)
        {
            Load_Database();
        }

        void Load_Database()
        {
            try
            {
                var db = new DbManager("DBConnection");
                db.SetCommand("SELECT name FROM master..sysdatabases");
                IDataReader dt = db.ExecuteReader();
                while (dt.Read())
                {
                    cmbDb.Items.Add(dt[0]);
                }
              
            }
            
            catch (SqlException ex)
            {
                MessageBox.Show("Lỗi: " + ex.Message);

            }
            catch (Exception ex1)
            {
                MessageBox.Show("Lỗi: " + ex1.Message);

            }
        }
      

    }
}
